import { Auths, Role } from './utils/types.d';

/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access(initialState: { currentUser?: API.CurrentUser | undefined }) {
  const { currentUser } = initialState || {};
  const ifSensorDeviceRead = currentUser?.auth?.sensorDevice?.includes(Auths.READ);
  const ifPatrolDeviceRead = currentUser?.auth?.patrolDevice?.includes(Auths.READ);
  const ifParkingLotRead = currentUser?.auth?.parkinglot?.includes(Auths.READ);
  const ifParkingspotRead = currentUser?.auth?.parkingspot?.includes(Auths.READ);
  const ifParkingRecordRead = currentUser?.auth?.parkingRecord?.includes(Auths.READ);
  const ifFailureReportRead = currentUser?.auth?.failureReport?.includes(Auths.READ);
  const ifWebUsr = currentUser?.auth?.webAppUser?.includes(Auths.READ);
  // const ifWechatRecharge = currentUser?.auth?.wechatUser?.includes(Auths.RECHARGE_READ);
  const ifPADRead = currentUser?.auth?.systemConfig?.includes(Auths.PDA_READ);
  const ifTemplateRead = currentUser?.auth?.systemConfig?.includes(Auths.TEMPLATE_READ);
  const ifStatsRead = currentUser?.auth?.systemConfig?.includes(Auths.STATS_READ);
  const ifAdRead = currentUser?.auth?.systemConfig?.includes(Auths.AD_READ);
  const ifAboRead = currentUser?.auth?.systemConfig?.includes(Auths.ABO_READ);
  const ifOrderConfigRead = currentUser?.auth?.systemConfig?.includes(Auths.ORDER_READ);
  const isAdmin = currentUser?.role === Role.ADMIN;
  return {
    canAdmin: isAdmin,
    canReadSystemUser: currentUser?.auth?.systemUser?.includes(Auths.READ),
    canReadSysRecord: currentUser?.auth?.sysRecord?.includes(Auths.READ),
    canReadDevice: ifSensorDeviceRead || ifPatrolDeviceRead,
    canReadSensorDeivce: ifSensorDeviceRead,
    canReadPatrolDevice: ifPatrolDeviceRead,
    canReadParking:
      isAdmin ||
      ifParkingLotRead ||
      ifParkingspotRead ||
      ifParkingRecordRead ||
      ifFailureReportRead,
    canReadParkinglot: ifParkingLotRead,
    canReadParkingspot: ifParkingspotRead,
    canReadParkingRecord: ifParkingRecordRead,
    canReadFailureReport: ifFailureReportRead,
    canReadBatchTask: currentUser?.auth?.batchTask?.includes(Auths.READ),
    canReadChargeRule: currentUser?.auth?.chargeRule?.includes(Auths.READ),
    canReadParkingOrder: currentUser?.auth?.parkingOrder?.includes(Auths.READ),
    canReadTollCollector: currentUser?.auth?.tollCollector?.includes(Auths.READ),
    canReadMember: currentUser?.auth?.member?.includes(Auths.READ),
    // canReadWechatUser: currentUser?.auth?.wechatUser?.includes(Auths.READ),
    canReadWechatRecharge: currentUser?.auth?.wechatUser?.includes(Auths.RECHARGE_READ),
    // canReadArrearsRecovery: ifWechatRecharge,
    canReadRevenue: currentUser?.auth?.revenue?.includes(Auths.READ),
    canReadWebUser: ifWebUsr,
    canReadInvoice: currentUser?.auth?.invoice?.includes(Auths.READ),
    canReadAbnormalAppeal: currentUser?.auth?.abnormalAppeal?.includes(Auths.READ),
    canReadPackages: currentUser?.auth?.packageManager?.includes(Auths.READ),
    canReadPDAConfig: isAdmin || ifPADRead,
    canReadTemplate: isAdmin || ifTemplateRead,
    canReadOrderConfig: isAdmin || ifOrderConfigRead,
    canEditAuth: isAdmin || currentUser?.role === Role.ENTERPRISE,
    canMerge: currentUser?.auth.mergeOrder?.includes(Auths.READ),
    canWechat: isAdmin || ifWebUsr,
    canReadShift: currentUser?.auth?.scheduleManagement?.includes(Auths.READ),
    canReadSystemConfig: isAdmin || ifPADRead || ifStatsRead || ifTemplateRead,
    canReadStatsConfig: isAdmin || ifStatsRead,
    canReadAdConfig: isAdmin || ifAdRead,
    canReadAboConfig: isAdmin || ifAboRead,
    canReadAbnormal: isAdmin || currentUser?.auth?.abnormalOrder?.includes(Auths.READ),
  };
}
